package com.colorful.insuranceproject.utils;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lovely3x.common.utils.ALog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by handsome-Bob on 2017/9/11.
 */

public class StatusBarUtil {

    public static final String MEIZU = "Meizu";

    private final static boolean sIsMeiZu;

    public static final String XIAOMI = "Xiaomi";

    private static final boolean sIsMiui;

    private static String sMiuiVersion;

    private static String sNavBarOverride;

    static {
        // Android allows a system property to override the presence of the navigation bar.
        // Used by the emulator.
        // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076

        sIsMiui = XIAOMI.equals(Build.BRAND);
        sIsMeiZu = MEIZU.equals(android.os.Build.BRAND);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);

                if (sIsMiui) {
                    try {
                        sMiuiVersion = (String) m.invoke(c, "ro.miui.ui.version.name");
                        if (TextUtils.isEmpty(sMiuiVersion)) sMiuiVersion = null;
                    } catch (Exception e) {
                        sMiuiVersion = null;
                    }
                }

                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
                sNavBarOverride = null;
            }
        }
    }

    private static void compatMeiZuAndXiaoMiStatusBarDarkMode(boolean darkMode, Activity activity) {
        if (sIsMiui) {
            Class<? extends Window> clazz = activity.getWindow().getClass();
            try {
                int darkModeFlag = 0;
                Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                extraFlagField.invoke(activity.getWindow(), darkMode ? darkModeFlag : 0, darkModeFlag);
            } catch (Exception e) {
                ALog.e("SBTM", e);
            }
        } else if (sIsMeiZu) {
            if (activity != null) {
                try {
                    Window window = activity.getWindow();
                    WindowManager.LayoutParams lp = window.getAttributes();
                    Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                    Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
                    darkFlag.setAccessible(true);
                    meizuFlags.setAccessible(true);
                    int bit = darkFlag.getInt(null);
                    int value = meizuFlags.getInt(lp);
                    if (darkMode) {
                        value |= bit;
                    } else {
                        value &= ~bit;
                    }
                    meizuFlags.setInt(lp, value);
                    window.setAttributes(lp);
                } catch (Exception e) {
                    ALog.e("SBTM", e);
                }
            }
        }
    }
    /**
     * 将状态栏设置为透明，并且内容为全屏
     *
     * @param activity
     * @param isLightStatusBar 是否是高亮色的状态栏，如果是高亮色的状态栏，则状态栏的文字和图标为深色，否则为浅色
     */
    public static void transparentAndFullStatusBar(Activity activity, boolean isLightStatusBar) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = activity.getWindow();
            window.clearFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                            | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            if (isLightStatusBar) {
                window.getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                );
            } else {
                window.getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            StatusBarUtil.compatMeiZuAndXiaoMiStatusBarDarkMode(isLightStatusBar, activity);
        }
    }

}
